package com.lanou.zy.projecta.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.activity.WebViewActivity;
import com.lanou.zy.projecta.adapter.SrnRecyclerAdapter;
import com.lanou.zy.projecta.entity.Srn;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.util.StringUtil;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class SrnFragment extends Fragment {
    private RecyclerView searchResultNote_RecyclerView;
    private SrnRecyclerAdapter srnRecyclerAdapter;
    private View v;
    private RequestQueue queue;
    private List<Srn.DataEntity.EntryEntity> sde;

    public static SrnFragment newInstance(String name) {
        SrnFragment s = new SrnFragment();
        Bundle args = new Bundle();
        args.putString("name", name);
        s.setArguments(args);
        return s;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_search_result_notes, null);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        srnRecyclerAdapter = new SrnRecyclerAdapter(getActivity());
        searchResultNote_RecyclerView = (RecyclerView) v.findViewById(R.id.searchResultNote_RecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        searchResultNote_RecyclerView.setLayoutManager(gridLayoutManager);
        searchResultNote_RecyclerView.setAdapter(srnRecyclerAdapter);

        srnRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url", sde.get(position).getView_url());
                intent.putExtra("name", sde.get(position).getTitle());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }


    private void initData() {

        queue = Volley.newRequestQueue(getActivity());
        String keyword = StringUtil.utf8Encode(getArguments().getString("name"));
        String url = "http://open.qyer.com/qyer/search/index?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.883734&lon=121.544912&type=thread&keyword=" + keyword + "&count=20&page=1";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Srn s = gson.fromJson(response, Srn.class);
                Srn.DataEntity sd = s.getData();
                sde = sd.getEntry();
                srnRecyclerAdapter.addData(sde);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);


    }
}
